//
//  YYECourseApplyViewController.m
//  YuYingEDU
//
//  Created by 胡波 on 2018/1/4.
//  Copyright © 2018年 胡波. All rights reserved.
//

#import "YYECourseApplyViewController.h"
#import "YYEIntrolFirstCell.h"
#import "YYEIntrolSecondCell.h"
#import "YYEIntrolThirdCell.h"
#import "YYELoginViewController.h"

@interface YYECourseApplyViewController () <UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) YYEBaseTableView *tableView;
@property (nonatomic,strong) UIButton *applyBtn;
@property (nonatomic,strong) NSString *apply;
@property (nonatomic,strong) YYECourseIntroModel *model1;
@property (nonatomic,strong) UIImageView *contentIV;

@end

@implementation YYECourseApplyViewController

static NSString * intrlCell = @"introlCell";
static NSString * courseCell = @"courseCell";
static NSString * teacherCell = @"teacherCell";
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    // 立即报名
    
    [self configUI];
    
//    [self configNavi];
    
    [self details];
    UIButton *backBtn = [[UIButton alloc]init];
    [backBtn setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    [backBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:backBtn];
    
    [backBtn makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.left).offset(12);
        make.top.equalTo(self.view.top).offset(kStatusBarHeight+12);
        make.size.equalTo(CGSizeMake(30, 30));
    }];
    
}

- (void)backAction
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)details {
    
    NSDictionary *dic = @{
                          @"uid":[LoginModel shareLogin].uid,
                          @"username":[LoginModel shareLogin].username,
                          @"token":[LoginModel shareLogin].token,
                          @"fid":_model.id
                          };
    [SVProgressHUD show];
    [AFNHelper post:Details parameters:dic success:^(id responseObject) {
        
        //        NSLog(@"%@",responseObject);
        if ([responseObject[@"status"] isEqual:@1]) {
            
            self.apply = responseObject[@"apply"];
            
            YYECourseIntroModel *model1 = [YYECourseIntroModel mj_objectWithKeyValues:responseObject[@"data"][0]];
            self.model1 = model1;
            
            if (self.apply.integerValue == 1) {
                [self.applyBtn setTitle:@"已报名" forState:UIControlStateNormal];
                self.applyBtn.userInteractionEnabled = NO;
            }
            
            
            [self.tableView reloadData];
            
        }
        [SVProgressHUD showWithStatus:responseObject[@"message"]];
        [SVProgressHUD dismiss];
        
    } faliure:^(id error) {
        
        NSLog(@"请求失败");
        [SVProgressHUD dismiss];
        
    }];
    
}

- (void)applyLession {
    
    if ([LoginModel shareLogin].token.length == 0) {
        
        YYELoginViewController *vc = [[YYELoginViewController alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
        return;
    }
    

    NSDictionary *dic = @{
                          @"uid":[LoginModel shareLogin].uid,
                          @"username":[LoginModel shareLogin].username,
                          @"token":[LoginModel shareLogin].token,
                          @"fid":_model.id
                          };
    [SVProgressHUD show];
    [AFNHelper post:Apply parameters:dic success:^(id responseObject) {
        
        //        NSLog(@"%@",responseObject);
        if ([responseObject[@"status"] isEqual:@1]) {
            
            
            [self.applyBtn setTitle:@"已报名" forState:UIControlStateNormal];
            self.applyBtn.userInteractionEnabled = NO;
            
        }
        [SVProgressHUD showWithStatus:responseObject[@"message"]];
        [SVProgressHUD dismiss];
        
    } faliure:^(id error) {
        
        NSLog(@"请求失败");
        [SVProgressHUD dismiss];
        
    }];
    
}

//- (void)configNavi {
//
//    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"morePoint"] style:UIBarButtonItemStyleDone target:self action:@selector(collectionClicked)];
//
//}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    

    [self.navigationController setNavigationBarHidden:YES animated:NO];

}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    [self.navigationController setNavigationBarHidden:NO animated:NO];

}

- (void) configUI {
    
    [self.view addSubview:self.tableView];
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.tableHeaderView = [self headerView];
    
    [self.tableView registerClass:[YYEIntrolFirstCell class] forCellReuseIdentifier:intrlCell];
    [self.tableView registerClass:[YYEIntrolSecondCell class] forCellReuseIdentifier:courseCell];
    [self.tableView registerClass:[YYEIntrolThirdCell class] forCellReuseIdentifier:teacherCell];
    
    self.applyBtn = [[UIButton alloc]init];
    [self.applyBtn setTitle:@"立即报名" forState:UIControlStateNormal];
    [self.applyBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.applyBtn setBackgroundColor:GeneralBlueColor];
    self.applyBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [self.view addSubview:self.applyBtn];
    [self.applyBtn addTarget:self action:@selector(applyLession) forControlEvents:UIControlEventTouchUpInside];

    
    [self.tableView makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(UIEdgeInsetsMake(0, 0, 45, 0));
    }];
    
    [self.applyBtn makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.view);
        make.height.equalTo(45);
    }];
    
}

- (UIView *)headerView {
    
    UIImageView *contentIV = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_WIDTH * 0.55)];
    contentIV.backgroundColor = GeneralBlueColor;
    self.contentIV = contentIV;
    [self.contentIV sd_setImageWithURL:[NSURL URLWithString:_model.img] placeholderImage:nil];

    return contentIV;
    
}

- (YYEBaseTableView *)tableView {
    
    if (!_tableView) {
        _tableView = [[YYEBaseTableView alloc]initwithDelegate:self];
    }
    return _tableView;
    
}

#pragma mark -- UITableView
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 3;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 1;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        
        YYEIntrolFirstCell *cell = [tableView dequeueReusableCellWithIdentifier:intrlCell];
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.model  = self.model1;
        return cell;
        
    }else if (indexPath.section == 1) {
        
        YYEIntrolSecondCell *cell = [tableView dequeueReusableCellWithIdentifier:courseCell];
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.model  = self.model1;

        return cell;
        
    }else {
        
        YYEIntrolThirdCell *cell = [tableView dequeueReusableCellWithIdentifier:teacherCell];
        cell.model  = self.model1;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        return cell;
        
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        return 84;
    }else if (indexPath.section == 1) {
        return 66;
    }else {
        return 133.0;
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    if (section == 0) {
        return 0.01;
    }else {
        return 40;
    }
    
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    UIView *contentView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40)];
    contentView.backgroundColor = GeneralBackColor;
    
    UIImageView *backIV = [[UIImageView alloc]init];
    backIV.image = [UIImage imageNamed:@"topBorder"];
    [contentView addSubview:backIV];
    
    UILabel *nameLabel = [[UILabel alloc]init];
    nameLabel.textColor = [UIColor blackColor];
    nameLabel.font = [UIFont systemFontOfSize:14];
    [backIV addSubview:nameLabel];
    
    [backIV makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(UIEdgeInsetsMake(0, 8, 0, 8));
    }];
    
    [nameLabel makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(backIV.left).offset(10);
        make.centerY.equalTo(backIV.centerY);
    }];
    
    if (section == 0) {
        
    }else if (section == 1) {
        
        nameLabel.text = @"课程介绍";
        
    }else {
        
        nameLabel.text = @"任课教师";
        
    }
    
    return contentView;
    
}

#pragma mark -- Clicked
// 收藏
- (void)collectionClicked {
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
